/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.tabbedproperties.composite;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.VisibilityKind;

import com.cea.papyrus.core.commands.factories.GenericCommandFactory;
import com.cea.papyrus.core.editor.MultiDiagramEditor;
import com.cea.papyrus.core.tabbedproperties.composite.ISectionComposite;

/**
 * 
 * 
 * @author Remi SCHNEKENBURGER
 */
public class VisibilityComposite implements ISectionComposite {


	/**
	 * 
	 */
	private Button checkBoxPublic = null;
	
	/**
	 * 
	 */
	private Button checkBoxProtected = null;
	
	/**
	 * 
	 */
	private Button checkBoxPrivate = null;
	
	/**
	 * 
	 */
	private Button checkBoxPackage = null;

	/**
	 * 
	 */
	private VisibilityKind currentVisibility = VisibilityKind.PUBLIC_LITERAL;

	/**
	 * 
	 */
	private Element element;
	
	/**
	 * 
	 */
	private DirectEditSelectionListener publicSelectionListener = new DirectEditSelectionListener(VisibilityKind.PUBLIC_LITERAL);
	
	/**
	 * 
	 */
	private DirectEditSelectionListener privateSelectionListener = new DirectEditSelectionListener(VisibilityKind.PRIVATE_LITERAL);
	
	/**
	 * 
	 */
	private DirectEditSelectionListener protectedSelectionListener = new DirectEditSelectionListener(VisibilityKind.PROTECTED_LITERAL);
	
	/**
	 * 
	 */
	private DirectEditSelectionListener packageSelectionListener = new DirectEditSelectionListener(VisibilityKind.PACKAGE_LITERAL);
	
	/**
	 * 
	 * 
	 * @return the currentVisibility
	 */
	public VisibilityKind getCurrentVisibility() {
		return currentVisibility;
	}

	/**
	 * 
	 * 
	 * @param currentVisibility the currentVisibility to set
	 */
	public void setCurrentVisibility(VisibilityKind currentVisibility) {
		this.currentVisibility = currentVisibility;
	}

	/**
	 * 
	 * 
	 * @return the element
	 */
	public Element getElement() {
		return element;
	}

	/**
	 * 
	 * 
	 * @param element the element to set
	 */
	public void setElement(Element element) {
		this.element = element;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.ISectionComposite#createContent(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory)
	 */
	/**
	 * 
	 * 
	 * @param factory 
	 * @param parent 
	 * 
	 * @return 
	 */
	public Composite createContent(Composite parent, TabbedPropertySheetWidgetFactory factory) {
		Composite composite = factory.createFlatFormComposite(parent);
		FormData data;

		// PUBLIC Visibility
		checkBoxPublic = factory.createButton(composite, "public", SWT.RADIO);
		data = new FormData();
		data.left = new FormAttachment(0, 85);
		data.top = new FormAttachment(0, 0);
		checkBoxPublic.setLayoutData(data);
		checkBoxPublic.addSelectionListener(publicSelectionListener);
		

		// PROTECTED Visibility
		checkBoxProtected = factory.createButton(composite, "protected", SWT.RADIO);
		data = new FormData();
		data.left = new FormAttachment(checkBoxPublic, 20);
		data.top = new FormAttachment(0, 0);
		checkBoxProtected.setLayoutData(data);
		checkBoxProtected.addSelectionListener(protectedSelectionListener);
		

		// PRIVATE Visibility
		checkBoxPrivate = factory.createButton(composite, "private", SWT.RADIO);
		data = new FormData();
		data.left = new FormAttachment(checkBoxProtected, 20);
		data.top = new FormAttachment(0, 0);
		checkBoxPrivate.setLayoutData(data);
		checkBoxPrivate.addSelectionListener(privateSelectionListener);
		
		// PACKAGE Visibility
		checkBoxPackage = factory.createButton(composite, "package", SWT.RADIO);
		data = new FormData();
		data.left = new FormAttachment(checkBoxPrivate, 20);
		data.right = new FormAttachment(100, 0);
		data.top = new FormAttachment(0, 0);
		checkBoxPackage.setLayoutData(data);
		checkBoxPackage.addSelectionListener(packageSelectionListener);

		CLabel visibilityLabel = factory.createCLabel(composite, "Visibility:"); //$NON-NLS-1$
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(checkBoxPublic,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(checkBoxPublic, 0, SWT.CENTER);
		visibilityLabel.setLayoutData(data);

		return composite;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.ISectionComposite#refresh()
	 */
	/**
	 * 
	 */
	public void refresh() {
		if(!checkBoxPackage.isDisposed() && !checkBoxPrivate.isDisposed() &&
				!checkBoxProtected.isDisposed() && !checkBoxPublic.isDisposed()){
			// remove all listeners
			checkBoxPublic.removeSelectionListener(publicSelectionListener);
			checkBoxPrivate.removeSelectionListener(privateSelectionListener);
			checkBoxPackage.removeSelectionListener(packageSelectionListener);
			checkBoxProtected.removeSelectionListener(protectedSelectionListener);

			if (element instanceof NamedElement){
				currentVisibility = ((NamedElement)element).getVisibility();
			}
			checkBoxProtected.setSelection(VisibilityKind.PROTECTED_LITERAL.equals(currentVisibility));
			checkBoxPrivate.setSelection(VisibilityKind.PRIVATE_LITERAL.equals(currentVisibility));
			checkBoxPublic.setSelection(VisibilityKind.PUBLIC_LITERAL.equals(currentVisibility));
			checkBoxPackage.setSelection(VisibilityKind.PACKAGE_LITERAL.equals(currentVisibility));

			checkBoxPublic.addSelectionListener(publicSelectionListener);
			checkBoxPrivate.addSelectionListener(privateSelectionListener);
			checkBoxPackage.addSelectionListener(packageSelectionListener);
			checkBoxProtected.addSelectionListener(protectedSelectionListener);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.ISectionComposite#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		
		if(checkBoxPublic!=null && checkBoxPrivate!=null && checkBoxPackage!=null && checkBoxProtected!=null &&
				!checkBoxPublic.isDisposed() && !checkBoxPrivate.isDisposed() && !checkBoxPackage.isDisposed() && !checkBoxProtected.isDisposed()){
			checkBoxPublic.removeSelectionListener(publicSelectionListener);
			checkBoxPrivate.removeSelectionListener(privateSelectionListener);
			checkBoxPackage.removeSelectionListener(packageSelectionListener);
			checkBoxProtected.removeSelectionListener(protectedSelectionListener);
		}
		
	}
	
	/**
	 * Listener for a direct edit manager.
	 * 
	 * @author Remi SCHNEKENBURGER
	 */
	protected class DirectEditSelectionListener implements org.eclipse.swt.events.SelectionListener {
		
		/**
		 * 
		 */
		private VisibilityKind visibilityKind;
		
		/**
		 * 
		 * 
		 * @param visibilityKind 
		 */
		DirectEditSelectionListener(VisibilityKind visibilityKind) {
			this.visibilityKind = visibilityKind;
		}
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetSelected(SelectionEvent e) {
			if (element instanceof NamedElement) {
				setCurrentVisibility(visibilityKind);
				Command command = GenericCommandFactory.eINSTANCE.createCommand(element, "Visibility", new Object[] {getCurrentVisibility()});
				// Darpan
				IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
				CommandStack stack = (CommandStack)editor.getAdapter(CommandStack.class);
				stack.execute(command);
			}
		}

		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetDefaultSelected(SelectionEvent e){
		}
	};
}
